/*
 * load_platfrom_lib.c
 *
 *  Created on: 2024-4-15
 *      Author: NI
 */
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>
#include "load_platfrom_lib.h"

capture_api_t api_tab;

static int load_lib(capture_api_t *api_tab, const char *lib_name )
{
	void *handle = dlopen( lib_name, RTLD_LOCAL );
	if( !handle ) {
		//logmsg( "dlopen('%s') failed: %s\n", lib_name, dlerror() );
		return EIO;
	}


#define	GETSYM( h, t, s )	if( NULL == ((t)->s = dlsym( h, #s )) ) { \
	/*		logmsg( "unresolved symbol '" #s "' in '%s'\n", lib_name );\*/	\
		dlclose( h ); \
		return ENOTSUP; \
	}

	api_tab->dll_handle = handle;
	GETSYM( handle, api_tab, capture_create_context );
	GETSYM( handle, api_tab, capture_destroy_context );
	GETSYM( handle, api_tab, capture_is_property );
	GETSYM( handle, api_tab, capture_update );
	GETSYM( handle, api_tab, capture_set_property_i );
	GETSYM( handle, api_tab, capture_set_property_p );
	GETSYM( handle, api_tab, capture_get_property_i );
	GETSYM( handle, api_tab, capture_get_property_p );
	GETSYM( handle, api_tab, capture_get_frame );
	GETSYM( handle, api_tab, capture_release_frame );

	return EOK;
}

int load_platfrom_lib(){
	return load_lib(&api_tab, PLATFROM_LIB_NAME);
}

int unload_platfrom_lib(){
	dlclose((void *)api_tab.dll_handle);
	return EOK;
}
